package edu.wpi.cs509.serverlogic;

import java.util.List;

import edu.wpi.cs509.persistence.PersistenceDatabase;
import edu.wpi.cs509.representation.UserName;
import edu.wpi.cs509.representation.UserStatistics;

public class UserStats {
	
	/** Gets a leader board based on the rank requested 
	 * 
	 * @param rank - rank that the leader board starts at
	 * @param count - Number of places in the leader board to fetch
	 * 
	 * @return boardList - a List object that is the leader board
	 * @throws Exception 
	 */
	public List<UserStatistics> getLeaderBoard(int rank, int count) throws Exception
	{
		return PersistenceDatabase.getHighScoresList(rank, count);
	}
	
	/** Get a leader board based on a username
	 * 
	 * @param username - User name the leaderboard starts with
	 * @param count - number of entries in the leader board
	 * 
	 * @return - a leaderboard with count entries based on the username input
	 * @throws Exception 
	 */
	public List<UserStatistics> getLeaderBoard(UserName username, int count) throws Exception
	{	
		return PersistenceDatabase.getHighScoresList(username.getUsername(), count);
	}
	
	/** Fetches a single users stats
	 * 
	 * @param username - username of stats to fetch
	 * 
	 * @return - a user's statistics 
	 */
	public UserStatistics getPersonalStats(UserName username)
	{
		return PersistenceDatabase.getUserStatistics(username.getUsername());
	}
	
	/**
	 * Deletes a user from the leaderboard
	 * 
	 * @param username - username to be deleted
	 */
	public void deleteUserStats(UserName username) throws Exception
	{
		PersistenceDatabase.removeUserAccount(username.getUsername());
	}
	
	/**
	 * update the number of games won by user
	 * 
	 * @param strUser - string of username to update
	 */
	public void updateGamesWon(String strUser){
		// Fetch user from list
		UserStatistics user1 = PersistenceDatabase.getUserStatistics(strUser);
		
		// Increment the users win count
		user1.incrementGamesWon();
	}
	
	/**
	 * update games lost by user
	 * 
	 * @param strUser - string of username to update
	 */
	public void updateGamesLost(String strUser){
		// Fetch user from list
		UserStatistics user1 = PersistenceDatabase.getUserStatistics(strUser);
		user1.incrementGamesLost();
	}
	
	/**
	 * update the number of games quit by the user
	 * 
	 * @param strUser - string of username to update
	 */
	public void incrementGamesQuit(String strUser){
		// Fetch user from list
		UserStatistics user1 = PersistenceDatabase.getUserStatistics(strUser);
		user1.incrementGamesLost();
	}
	
	/**
	 * update the number of games watched by the user
	 * 
	 * @param strUser - string of the username to update
	 */
	public void incrementGamesWatched(String strUser){
		// Fetch user from list
		UserStatistics user1 = PersistenceDatabase.getUserStatistics(strUser);
		user1.incrementGamesWatched();
	}
	
	/**
	 * update the lowest number of moves it took a user to win
	 * 
	 * @param strUser - string of username to update
	 */
	public void incrementWonLeastMoves(String strUser){
		// Fetch user from list
		UserStatistics user1 = PersistenceDatabase.getUserStatistics(strUser);
		int nMoves = 0;
		//int nMoves = getTotalMoves();
		user1.setWonLeastMoves(nMoves);
	}
	
	/**
	 * Takes in the username of a user, and returns their rank
	 * @param user - username of player to look up
	 * 
	 * @return - rank of player as an integer
	 */
	public int getRankFromUsername(UserName user){
		return PersistenceDatabase.getRank(user.getUsername());
	}
}
